%%%% Compare the inductor value requirement for buck converter
%%%% and the SC enhanced regulator.
    % inductor reduction ratio, comes from 3 components.
    % D_ratio: less extreme duty ratio
    % f_ratio: increased switching frequency for same switching loss
    % R_ratio: assuming same overal conduction loss
%%%% assumption: 1. operating at the boundary of ccm and dcm  
close all;
clc;
clear;

%% Parameters
Vout=1;          % unity output voltage
Vin=2:0.1:12*Vout;    % input voltage sweep
Rload=1;       % Load resistance
Iout=Vout/Rload;
alpha=1;        % percentage output current ripple +-alpha
I_ripple=alpha*2*Iout;  % peak to peak ripple
f0=100e6;         % switching frequency
cc=hsv(5);      % color map
f1=(Vout./Vin).^2*f0;
N_start=2;
N_step=1;
N_end=5;

%% First compares single buck at different Vin
figure;
L1=(1-Vout./Vin).*Vout./(I_ripple.*f1);
plot(Vin,L1*1e6,'LineWidth',1.5);
E_L1=0.5*(1-Vout./Vin).*Vout./(I_ripple.*f1)*(Iout+I_ripple/2).^2;    % Energy
hold on;
xlabel('Conversion ratio V_{in}/V_{out}')
ylabel('Inductor Value [\muH]')
set_figure_style;
resize_figure(200,50)
% print('-depsc2', 'single_inductor');

%% Now consider only a second stage buck
figure
legendInfo=[]
for N=N_start:N_step:N_end     % N:1 is the conversion ratio of the first stage
    L2=(1-Vout./Vin*N).*Vout./(N^2*I_ripple.*f1);
    index=find(Vin/Vout >= N);
    plot(Vin(index:end),L2(index:end)./L1(index:end),'color',cc(N-1,:),'LineWidth',1.5);
    legendInfo = [legendInfo;sprintf('N= %i',N)];
    hold on;
end
ylim([0 0.25])
xlabel('Conversion ratio V_{in}/V_{out}')
ylabel('Normalized Inductor Value')
set_figure_style;
legend(legendInfo,'Location','NorthEast');
resize_figure(200,50)
% print('-depsc2', 'ideal');

%% buck + buck 
figure;
for N=N_start:N_step:N_end      % N:1 is the conversion ratio of the first stage
    N2=Vin/Vout/N;  % N2 is the conversion ratio of the second stage
    L_BB_1=(1-1/N).*(Vin/Vout/N)*Vout./(f1*I_ripple.*N);
    L_BB_2=(1-Vout./Vin*N).*Vout./(N^2*I_ripple.*f1);
    L_BB=L_BB_1+L_BB_2;
    index=find(Vin/Vout >= N);
%     plot(Vin(index:end),L_BB_1(index:end),'--','color',cc(N-1,:),'LineWidth',1.5);
%     plot(Vin(index:end),L_BB_2(index:end),'--','color',cc(N-1,:),'LineWidth',1.5);
    % efficiency penalty
    L_BB=L_BB*4;
    plot(Vin(index:end),L_BB(index:end)./L1(index:end),'color',cc(N-1,:),'LineWidth',1.5);
    %legendInfo = [legendInfo;sprintf('N= %i',N)];
    hold on;
end
ylim([0 2])
xlabel('Conversion ratio V_{in}/V_{out}')
ylabel('Normalized Inductor Value')
set_figure_style;
legend(legendInfo,'Location','NorthEast');
resize_figure(200,50)
print('-depsc2', 'buck_buck');

%% Multilevel buck
figure;
for N=N_start:N_step:N_end      % N:1 is the conversion ratio of the first stage
    L_MLB=(1-Vout./Vin*N).*Vout./(N*I_ripple.*f1);
    index=find(Vin/Vout >= N);
    plot(Vin(index:end),L_MLB(index:end)./L1(index:end),'color',cc(N-1,:),'LineWidth',1.5);
    %legendInfo = [legendInfo;sprintf('N= %i',N)];
    hold on;
end
ylim([0 0.5])
xlabel('Conversion ratio V_{in}/V_{out}')
ylabel('Normalized Inductor Value')
set_figure_style;
legend(legendInfo,'Location','NorthEast');
resize_figure(200,50)
% print('-depsc2', 'multilevel');

%% Multilevel buck taking capacitor into account
figure;
for N=N_start:N_step:N_end      % N:1 is the conversion ratio of the first stage
    E_L_MLB=0.5*(1-Vout./Vin*N)*Vout./(N.*f1*I_ripple)*(Iout+I_ripple/2).^2;
    E_C_MLB=0.5*2*Iout*Vout*N*(N-1)*(2*N-1)/(6*N)./(f1*0.1);
    Volume=E_L_MLB+E_C_MLB/1000;
    index=find(Vin/Vout >= N);
    plot(Vin(index:end),(E_L_MLB(index:end)+E_C_MLB(index:end)/1000)./E_L1(index:end),'color',cc(N-1,:),'LineWidth',1.5);
    hold on;
    plot(Vin(index:end),E_L_MLB(index:end)./E_L1(index:end),'--','color',cc(N-1,:),'LineWidth',1.5);
end
ylim([0 0.5])
xlabel('Conversion ratio V_{in}/V_{out}')
ylabel('Normalized Inductor Volume')
set_figure_style;
legend(legendInfo,'Location','NorthEast');
resize_figure(200,50)
print('-depsc2', 'multilevel_capacitor');
break

% % sweep the conversion ratio, N_sc

%     D=N_sc*Vout./Vin;      % find the duty ratio for SC enhanced regulator
%     D_ratio=(1-Vout*N_sc./Vin)./(1-Vout./Vin);
% 
%     L_reduction_ratio=D_ratio;
%     index=find(Vin >= N_sc);
%     plot(Vin(index:end),L_reduction_ratio(index:end),'color',cc(N_sc-1,:),'LineWidth',1.5);
%     hold on;
% end
% legend('N=2','N=3','N=4','N=5','N=6','N=7','N=8','Location','NorthWest')
% xlabel('Conversion ratio, V_{in}/V_{out}')
% ylabel('Normalized inductance')
% % ylim([0 1])
% % xlim([2 40])
% grid on;
